<?php
/**
 * 
 * @author Freezy
 *
 */
class rewardPunishmentPerDepartemen extends MY_Controller {

	function rewardPunishmentPerDepartemen(){
		parent::MY_Controller();
		$this->validate_cookie();
	}
	
	function _remap(){
		//validate user first
		$URICount = $this->uri->total_segments();
		$usr = $this->uri->segment(2);	//get username
		$this->_validateUser($usr);

		//call function
		$func = $this->uri->segment(4);
		$params = array();
		for($i=5;$i<=$URICount; $i++){
			$params[] = $this->uri->segment($i);
		}
		
	    if ( method_exists($this, $func) ) {
	    	call_user_func_array( array($this, $func), $params);
	    }
	    else if (count($params)<=0){
			$this->index();
		}
	}
	
	function _translate_nav($nav, $userAlias){
		$nv = array();
		foreach ($nav as $title=>$value){
			$nv[$title] = array();
			foreach ($value as $item=>$val){
				$nv[$title][$item] = preg_replace("/(:user)/i",$userAlias, $val);
			}
		}
		return $nv;
	}
	
	function index(){
		$this->load->library('session');
		$usrAlias = md5($this->session->userdata('usrname'));
		
		$this->load->helper(array('html'));
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');
		$this->load->model('departemen_model');
		
		//layout first
		$layoutConf = $CONF['layout'];
		$layoutConf['left_content'] = array(
			'view'=>'template/horz_nav', 
			'view_data'=>array(
				'data'=>$usrAlias!=md5('admin')?$this->_translate_nav($CONF['nav']['users'],$usrAlias):$CONF['nav']['psdm']
			) 
		);
		$layoutConf['right_content'] = array('view'=>'graph/rewardPunishmentPerDepartemen/overview', 
			'view_data'=>array(
				'limit' => false,
				'usrAlias'=>$usrAlias,
				'depData'=>$this->departemen_model->select_all("dep_nama asc")
			));
		$this->initialize_layout($layoutConf);
		
		//page
		$pageConf = $CONF['page'];
		$pageConf['jscript_default'][] = 'graph/rewardPunishmentPerDepartemen';
		$pageConf['jscript'] = array_merge(
			$pageConf['jscript_default'],
			array('datepicker', 'graph/rewardPunishmentPerDepartemen','utils/searchDialog'));
		$pageConf['css'] = array_merge($pageConf['css_default'],array('datepicker'));
		$pageConf['template_view'] = 'template/2_layout';
		$this->initialize_page($pageConf);
		
		$this->finalize_view();		
	}

	function printable(){
		$this->load->library('session');
		$usrAlias = md5($this->session->userdata('usrname'));
		
		$this->load->library('session');
		$graphData = $this->session->userdata('graph_printable');
		
		$depTag = "";
		foreach($graphData['dep_id'] as $row){
			$depTag .= "&dep_id[]=$row"; 
		}
		
		$this->load->view('graph/rewardPunishmentPerDepartemen/printable', array('graph'=>
			"<img src=\"". 
				site_url(array('graph',$usrAlias,'rewardPunishmentPerDepartemen')) .
				"/getGraph?_jpg_csimd=1
				&periode1=$graphData[periode1]
				&periode2=$graphData[periode2]
				$depTag\">"
		));
	}
	
	function getGraph(){
		$qq = $_SERVER['QUERY_STRING'];
		if ($qq && $qq!=""){
			foreach(explode("&",$qq) as $tmp){
				$kv = explode("=",$tmp);
				$_GET[$kv[0]] = $kv[1];
			}
		}
		
		//printable version hack
		$this->load->library('session');
		$this->session->set_userdata('graph_printable', null);
		$this->session->set_userdata('graph_printable', $_REQUEST);
		
		include ("system/application/libraries/jpgraph/jpgraph.php");
		include ("system/application/libraries/jpgraph/jpgraph_bar.php");
		$this->load->helper('graph');
		$this->load->model(array('departemen_model','report_model'));
		
		//generate data
		$startPeriode = $_REQUEST['periode1'];
		$endPeriode = $_REQUEST['periode2'];
		$depID = $_REQUEST['dep_id'];
		if (!isset($depID) || !$startPeriode || !$endPeriode) { return; }
		
		$depID = is_array($depID)? $depID : array($depID);
		
		$graphData = array();
		foreach($depID as $dd){
			$ddd = $this->departemen_model->select_by_id($dd);
			$ddd = (array)$ddd->row();
			$graphData ['label'][] = $ddd['dep_nama'];
		}
		
		$CSIM = array();
		foreach ($depID as $dep){
			$tmp = $this->report_model->jumlahRewardPunishmentPerDepartemen($dep, $startPeriode, $endPeriode );
			$graphData['data']['pr'][] = $tmp['PR'];
			$CSIM['pr']['target'][] = "load_drillGraph(".json_encode(array(
					"type"=> '1',
					'deptID'=> $dep,
					'startPeriode'=> $startPeriode,
					'endPeriode'=> $endPeriode,
				)).")";
			$CSIM['pr']['alt'][] = "%0.1f";
	
			$graphData['data']['pe'][] = $tmp['PE'];
			$CSIM['pe']['target'][] = "load_drillGraph(".json_encode(array(
					"type"=> '-1',
					'deptID'=> $dep,
					'startPeriode'=> $startPeriode,
					'endPeriode'=> $endPeriode,
				)).")";
			$CSIM['pe']['alt'][] = "%0.1f";
		}
	
		//Create the bar plot	
		$bplot1 = new BarPlot($graphData['data']['pr']);
		$bplot1->SetFillColor("blue");
		$bplot1->SetWidth(0.5);
		$bplot1->SetLegend('Rata-rata Reward');
		$bplot1->SetCSIMTargets($CSIM['pr']['target'],$CSIM['pr']['alt']);
		$bplot1->value->Show();
		
		$bplot2 = new BarPlot($graphData['data']['pe']);
		$bplot2-> SetFillColor("red");
		$bplot2->SetWidth(0.5);
		$bplot2->SetLegend('Rata-rata Punishment');
		$bplot2->SetCSIMTargets($CSIM['pe']['target'],$CSIM['pe']['alt']);
		$bplot2->value->Show();
	
		// Create the graph.
		$graph = new Graph(700,500,"auto");
		$graph->SetFrame(false,'white@.5',1); 
		$graph->title->Set("Perolehan Reward dan Punishment Departemen per Departemen");
		$graph->SetColor("white"); 
		$graph->SetScale("textlin");
		$graph->img->SetMargin(80,30,40,80);
		$graph->img->SetAntiAliasing(); 
		
		$graph->ygrid->SetFill(true,'#FFFFFF@0.5','#d7e5f5@0.5');
		$graph->yaxis->SetFont(FF_FONT1,FS_BOLD);
		$graph->yaxis->SetPos("min");
		$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
		$graph->yaxis->title->set("Rata - rata");
		$graph->yaxis->SetTitleMargin(50); 
		
		$graph->xaxis->SetFont(FF_FONT1,FS_BOLD);
		$graph->xaxis->SetPos("min");
		$graph->xaxis->SetTickLabels($graphData['label']);
		$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
		$graph->xaxis->title->set("Departemen");
		if ( count($graphData['label'])>10 ) {
			$graph ->xaxis->SetTextLabelInterval(count($graphData['label'])/4);
		}
		
		$graph->legend->SetLayout(LEGEND_HOR);
		$graph->legend->Pos(0.4,0.95,"center","bottom");
		
		// Add the plot to the graph
		$graph->Add(new GroupBarPlot(array($bplot1 ,$bplot2)));
		
		// Display the graph
		$this->load->library('session');
		$usrName = $this->session->userdata('usrname');
		$usrAlias = md5($usrName);
		$graph->StrokeCSIM(site_url(array("graph",$usrAlias,"rewardPunishmentPerDepartemen","getGraph")));
	}
	
	function getDrillAvgReward($depID, $startPeriode, $endPeriode){
		$this->load->helper(array('html'));
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');
		
		$this->load->view('graph/rewardPunishmentPerDepartemen/drill-AvgReward',array(
			'dep_id'=>$depID,
			'startPeriode'=>$startPeriode, 
			'endPeriode'=>$endPeriode,
			'jenis_options'=>$CONF['achievement']['jenis_options'],
			'tingkat_options'=>array_merge(
				$CONF['detachievement']['tingkat_options']['PR'],
				$CONF['detachievement']['tingkat_options']['PE'])
		));
	}
	
	function getDrillAvgPunishment($depID, $startPeriode, $endPeriode){
		$this->load->helper(array('html'));
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');
		
		$this->load->view('graph/rewardPunishmentPerDepartemen/drill-AvgPunishment',array(
			'dep_id'=>$depID,
			'startPeriode'=>$startPeriode, 
			'endPeriode'=>$endPeriode,
			'jenis_options'=>$CONF['achievement']['jenis_options'],
			'tingkat_options'=>array_merge(
				$CONF['detachievement']['tingkat_options']['PR'],
				$CONF['detachievement']['tingkat_options']['PE'])
		));
	}
}
/* End of file graph/rewardPunishmentPerDepartemen.php */
/* Location: ./system/application/controllers/graph/rewardPunishmentPerDepartemen.php */